package imdbfolder.data;

import imdbfolder.gui.MovieController;

import java.io.File;
import java.util.Comparator;

public enum SortMode {
    ALPHABETICALLY("Sort alphabetically", new Comparator<MovieController>() {
        @Override
        public int compare(MovieController o1, MovieController o2) {
            return o1.getMovie().getTitle().compareTo(o2.getMovie().getTitle());
        }
    }), BY_SCORE("Sort by score", new Comparator<MovieController>() {
        @Override
        public int compare(MovieController o1, MovieController o2) {
            return o1.getMovie().getScore() < o2.getMovie().getScore() ? 1 : -1;
        }
    }), FILETIME("Sort by filetime", new Comparator<MovieController>() {
        @Override
        public int compare(MovieController o1, MovieController o2) {
            return new File(o1.getMovie().getFileName()).lastModified() < new File(o2.getMovie().getFileName()).lastModified() ? 1 : -1;
        }
    }), YEAR("Sort by release year", new Comparator<MovieController>() {
        @Override
        public int compare(MovieController o1, MovieController o2) {
        	if (o1.getMovie().getYear() < o2.getMovie().getYear())
        		  return 1;
        		else if (o1.getMovie().getYear() > o2.getMovie().getYear())
        		  return -1;
        		else
        		  return 0;
      // not working here, under strange circumstances ends up as exception in timsort-lib
      // return o1.getMovie().getYear() < o2.getMovie().getYear() ? 1 : -1;
        }
    });

    private String name;

    private final Comparator<MovieController> comparator;

    SortMode(String name, Comparator<MovieController> comparator) {
        this.name = name;
        this.comparator = comparator;
    }

    @Override
    public String toString() {
        return name;
    }

    public Comparator<MovieController> getComparator() {
        return comparator;
    }
}